package com.jvyou.app.ds.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jvyou.app.common.domain.dto.MenuDto;
import com.jvyou.app.common.domain.entity.Menu;
import com.jvyou.app.common.domain.model.PageResult;
import com.jvyou.app.common.domain.vo.MenuTreeVo;
import com.jvyou.app.common.utils.bean.BeanCopyUtils;
import com.jvyou.app.common.utils.orm.WrapperTool;
import com.jvyou.app.common.utils.tree.TreeUtils;
import com.jvyou.app.ds.mapper.MenuMapper;
import com.jvyou.app.ds.service.MenuService;
import org.springframework.stereotype.Service;

import java.util.List;


/**
 * 菜单权限表(Menu)表服务实现类
 *
 * @author 橘柚
 * @since 2025-03-05 05:48:21
 */
@Service("menuService")
public class MenuServiceImpl extends ServiceImpl<MenuMapper, Menu> implements MenuService {

    private QueryWrapper<Menu> getWrapper(MenuDto menuDto) {
        QueryWrapper<Menu> wrapper = new QueryWrapper<>();
        WrapperTool.notEmptyEq(wrapper, "route_name", menuDto.getRouteName());
        WrapperTool.notEmptyLike(wrapper, "name", menuDto.getName());
        WrapperTool.notEmptyLike(wrapper, "perms", menuDto.getPerms());
        WrapperTool.notEmptyEq(wrapper, "menu_type", menuDto.getMenuType());
        WrapperTool.notEmptyEq(wrapper, "status", menuDto.getStatus());
        if (menuDto.getRoleId() != null && menuDto.getRoleId() > 0) {
            WrapperTool.inSql(wrapper, "id", "select menu_id from sys_role_menu where role_id=?", menuDto.getRoleId());
        }
        return wrapper;
    }

    @Override
    public List<Menu> listMenu(MenuDto menuDto) {
        return list(getWrapper(menuDto));
    }

    @Override
    public PageResult<Menu> pageMenu(MenuDto menuDto) {
        IPage<Menu> page = new Page<>(menuDto.getCurrent(), menuDto.getPageSize());
        page = page(page, getWrapper(menuDto));
        return PageResult.success(page.getTotal(), page.getRecords());
    }

    /**
     * 查询树形结构的菜单
     *
     * @return 菜单列表
     */
    @Override
    public List<MenuTreeVo> treeMenu(String name, Long roleId) {
        LambdaQueryWrapper<Menu> wrapper = new LambdaQueryWrapper<>();
        wrapper.select(Menu::getId, Menu::getName, Menu::getParentId);
        WrapperTool.notEmptyLike(wrapper, Menu::getName, name);
        if (roleId != null && roleId > 0) {
            WrapperTool.inSql(wrapper, Menu::getId, "select menu_id from sys_role_menu where role_id=?", roleId);
        }

        wrapper.orderByAsc(Menu::getOrderNum);
        List<Menu> menuList = list(wrapper);
        List<MenuTreeVo> menuTreeVos = BeanCopyUtils.copyBeanList(menuList, MenuTreeVo.class);
        menuTreeVos = TreeUtils.generateTrees2(menuTreeVos);
        menuTreeVos.add(0,new MenuTreeVo(0L,0L,"顶级菜单",null));
        return menuTreeVos;
    }
}
